import requests
import json

# 服务器地址
base_url = "http://202.181.124.195:9945"
# 发送的数据（确保是有效的JSON格式）
test_data1 = {
    "data": {
        "type": "init",
        "nodeInitList": ["encrypted_data_1", "encrypted_data_2"],
        "initSignList": ["signature_1", "signature_2"],
        "initData": "init_data_example",
        "initDataSign": "init_data_signature",  # ***
        "cert": "cert_example"
    }
}
test_data = {
    "data": {
        "type": "init",
        "nodeInitList": ["encrypted_data_1", "encrypted_data_2"],
"initSignList": ["signature_1", "signature_2"],
"initData": "init_data_example",
"cert": "cert_example"
    }
}
# 将 test_data 转换为 JSON 字符串
json_data = json.dumps(test_data)

# 发送POST请求
response = requests.post(
    f'{base_url}/chain_change',
    data=json_data,  # 发送已经转换为字符串的 JSON 数据
    headers={'Content-Type': 'application/json'}  # 设置正确的Content-Type
)

print(f"Response Status Code: {response.status_code}")
# 如果响应内容是有效的JSON
if response.status_code == 200:
    try:
        # 尝试将响应体解析为JSON
        response_data = response.json()
        print(f"Response Data: {response_data}")
    except json.decoder.JSONDecodeError:
        print("Error: Response content is not valid JSON")
else:
    print(f"Error: Received an error response from the server, Status Code: {response.status_code}")
